package com.huopaolan.yohuoshop.Sp;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * SP保存状态
 */
public class SpUtils {
    private Context context;
    private SharedPreferences preferences;

    public void init(Context context) {
        this.context = context;
        preferences = context.getSharedPreferences("login", Context.MODE_PRIVATE);
    }

    //单例模式
    private static SpUtils spUtils;

    public SpUtils() {
    }

    public static SpUtils getInstance() {
        //双重锁
        if (spUtils == null) {
            synchronized (SpUtils.class) {
                if (spUtils == null) {
                    spUtils = new SpUtils();
                }
            }
        }
        return spUtils;
    }

    //写数据
    public void SpWrite(boolean flag) {
        //编辑对象
        SharedPreferences.Editor editor = preferences.edit();
        //写数据
        editor.putBoolean("LoginFlag", flag);
        //提交数据
        editor.commit();
    }

    //出入用户id
    public void SpWriteUser(String userName) {
        //编辑对象
        SharedPreferences.Editor editor = preferences.edit();
        //写入数据
        editor.putString("Nike_name", userName);
        //提交数据
        editor.commit();
    }

    //读数据
    public boolean SpRead() {
        return preferences.getBoolean("LoginFlag", false);
    }

    //读取用户id
    public String SpReadUser() {
        return preferences.getString("Nike_name", "");
    }
}
